Creating Movie Clip Subclasses in ActionScript 2.0
Table of Contents
Classes and Objects, Symbols and Clips
Movie clips exist as symbols in a movie's library. When a symbol in the library is placed on the Stage an instance of the symbol is created. During the life of the movie an instance may change its color, position, and other properties. The symbol in the library acts as a template or initial design for the clip. Once on the Stage, the instance itself can be changed using different techniques, including the following:
- Animating the clip on the stage's timeline using motion tweens
- Attaching behaviors or event handlers to the clip
- Altering the clip's properties using ActionScript in a timeline
Whatever you do to each instance, the symbol in the library does not change; it remains as a template for creating other clips using ActionScript. Each instance of the symbol is unique in that it may have a particular state at one time. For example, one may be visible and another may not be.
ActionScript classes and objects have a similar, though somewhat different,
relationship to each other as movie clip symbols and instances. An
ActionScript class defines the initial data (properties) and behaviors
(methods or functions) of an ActionScript object. Instead of creating
a movie clip symbol in the library using drawing tools, an ActionScript
class is created by writing some code called a class definition.
Instead of dragging a symbol onto the Stage, an instance of an ActionScript
class is created using the new keyword within a script.
In ActionScript 1.0 a definition for a class of objects is created using the prototype object. Here is a short example that shows the definition of a class and the creation of one object based on the class:
//Define the class in AS1 by defining a function:
function ScoreKeeperClass(nShapes){
this.nShapes = nShapes;
this.startTime = getTimer();
this.hits = 0;
}
// Give the class methods by attaching functions to the prototype object.
ScoreKeeperClass.prototype.addHit = function(){
this.hits++;
}
ScoreKeeperClass.prototype.getScore = function(){
return this.hits/this.nShapes;
}
// Create an object of the class using the new keyword.
scoreKeeper = new ScoreKeeperClass(10);
// Call some methods of the object.
scoreKeeper.addHit();
scoreKeeper.addHit();
trace("Your score is: " + scoreKeeper.getScore()); // Your score is: 0.2
Of course, other than printing something into the output window, this snippet of code just manipulates some data inside a score keeper object.
ActionScript 2.0 classes and objects are similar except that a class file containing slightly different code defines the class. For example, in a file named ScoreKeeper.as you might have this code:
// Define the class using the class keyword.
class ScoreKeeper{
// Define the properties of the class including their type.
var hits:Number = 0;
var startTime:Number;
var nShapes:Number;
// Define a constructor function to be called when a new object is created.
function ScoreKeeper(n){
nShapes = n;
startTime = getTimer();
}
// Define some methods/functions for each object of this class.
function addHit(){
hits++;
}
function getScore(){
return hits/nShapes;
}
}
To use this class you have these statements on the Timeline of your movie:
scoreKeeper = new ScoreKeeper(10);
scoreKeeper.addHit();
scoreKeeper.addHit();
trace("Your score is: " + scoreKeeper.getScore()); // Your score is: 0.2
ActionScript objects are instances of ActionScript classes, much as
movie clips on the Stage are instances of movie clip symbols in the
library. After an ActionScript object is created its properties (data)
may change over time just as the properties of a movie clip may change.
The important difference between classes and movie clip symbols is
that classes give objects a predefined set of behaviors that every
object of that class will have. For example, every ScoreKeeper object
will have the custom written getScore() method. Even in
Macromedia Flash 5 there was no simple way—like defining methods—to
create predefined behaviors for a movie clip symbol.